package com.power.sifa.approve.dao;

import com.power.sifa.approve.model.Aprv_materials_options;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 表单输入动态生成的材料文件 持久层(hibernate)
 */

public interface Aprv_materials_optionsDao extends JpaRepository<Aprv_materials_options, Serializable>, JpaSpecificationExecutor<Aprv_materials_options>, Repository<Aprv_materials_options, Serializable>, Aprv_materials_optionsDaoCustom<Aprv_materials_options, Serializable> {

   @Modifying
   @Transactional
   @Query("delete  from Aprv_materials_options s where  s.id in (:ids) ")
   void removeByIds(@Param("ids") List<String> ids);


   @Query(" from Aprv_materials_options s where s.applyid = :applyid ")
   List<Aprv_materials_options> findAllOptionsByApplyid(@Param("applyid") String applyid);


   @Query(" from Aprv_materials_options s where s.materialid = :materialId ")
   List<Aprv_materials_options> findAllOptionsByMaterialId(@Param("materialId") String materialId);

   @Query(" from Aprv_materials_options s where s.materialid in (:materialIds) order by s.version desc")
   List<Aprv_materials_options> findAllOptionsByMaterialIds(@Param("materialIds") List<String> materialIds);

   @Query(" from Aprv_materials_options s where s.attachid = :attachId ")
   List<Aprv_materials_options> findAllOptionsByAttachId(@Param("attachId") String attachId);


   @Query(" from Aprv_materials_options s where s.applyid = :applyid and s.mattype = :materialType ")
   List<Aprv_materials_options> findAllOptionsByMaterialType(@Param("applyid") String applyid,
                                                        @Param("materialType") Integer materialType );

   @Query(value = " select a.id, to_char(a.createtime, 'yyyy年MM月d日') as createtime, c.path from aprv_materials_options a left join Aprv_process_materials b on a.materialid = b.id left join ha_attachment c on b.attachid  = c.id " +
           "where a.applyid = :applyid and a.mattype in (:mattypeIds)", nativeQuery = true)
   List<Map<String, Object>> getByApplyidAndMattype(String applyid, List<Integer> mattypeIds);

   @Query(value = " select a.id, to_char(a.createtime, 'yyyy年MM月d日') as createtime, c.path from aprv_materials_options a left join Aprv_process_materials b on a.materialid = b.id left join ha_attachment c on b.attachid  = c.id " +
           "where a.applyid = :applyid", nativeQuery = true)
   List<Map<String, Object>> getByApplyd(String applyid);

   @Query(value = " select Max(version) from Aprv_materials_options s where s.materialid = :materialId", nativeQuery = true)
   Integer findMaxVersion (@Param("materialId") String materialId);

}
